package com.lelefly.base.serviceImp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.lelefly.base.Validator.FarmlandInfoValidator;
import com.lelefly.base.daoimp.BaseDao;
import com.lelefly.base.model.Result;
import com.lelefly.base.model.ValidateErrors;
import com.lelefly.base.po.FarmlandInfo;
import com.lelefly.constant.Constant;
import com.lelefly.handleexecption.ServiceExecption;
import com.lelefly.handleexecption.ValidateExecption;
@Service
public class FarmlandInfoService extends BaseService{

	@Resource
	public BaseDao baseDao;
	
	@Resource
	public FarmlandInfoValidator farmlandInfoValidator;
	
	@SuppressWarnings({ "null", "rawtypes" })
	public Result getFarmlandInfos(String usernm) {
		Result<FarmlandInfo> result = new Result<>();
		try {
			if (StringUtils.isEmpty(usernm) ) {
				throw new ServiceExecption("参数错误");
			}
			String hql = "from FarmlandInfo as mdeol where usernm='"+usernm+"' ";
			List<FarmlandInfo> farmlandInfos= baseDao.queryObjetReList(hql);
			result.setState(Constant.HTTP_STATUS_OK);
			result.setResultList(farmlandInfos);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ServiceExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
	
	@SuppressWarnings("null")
	public Result<FarmlandInfo> addFarmlandInfo(FarmlandInfo farmlandInfo) {
		Result<FarmlandInfo> result = new Result<>();
		try {
			ValidateErrors validateErrors = new ValidateErrors();
			farmlandInfoValidator.validator(farmlandInfo, validateErrors);
			if (validateErrors.isHasError()) {
				throw new ValidateExecption(validateErrors.getStackFirstError());
			}
			String sql ="from FarmlandInfo as model where farmlandname ='"
			               +farmlandInfo.getFarmlandname()+"' and usernm ='"+farmlandInfo.getUsernm()+"' ";
			FarmlandInfo fInfo = (FarmlandInfo) baseDao.getObjcetReUnique(sql);
			if (fInfo!=null) {
				throw new ValidateExecption("该农田名称已经注册");
			}
			baseDao.saveOrUpdateObject(farmlandInfo);
			result.setState(Constant.HTTP_STATUS_OK);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ValidateExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
	
	
	@SuppressWarnings("null")
	public Result<FarmlandInfo> updateFarmlandInfo(FarmlandInfo farmlandInfo) {
		Result<FarmlandInfo> result = new Result<>();
		try {
			ValidateErrors validateErrors = new ValidateErrors();
			farmlandInfoValidator.validator(farmlandInfo, validateErrors);
			if (validateErrors.isHasError()) {
				throw new ValidateExecption(validateErrors.getStackFirstError());
			}
			baseDao.UpdateObject(farmlandInfo);
			result.setState(Constant.HTTP_STATUS_OK);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ValidateExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
	
	@SuppressWarnings("null")
	public Result delFarmlandInfo(String farmlandnm ) {
		Result<FarmlandInfo> result = new Result<>();
		try {
			if (StringUtils.isEmpty(farmlandnm)) {
				throw new ServiceExecption("删除条件为空");
			}
			String sql = "delete FarmlandInfo as model where farmlandnm='"+farmlandnm+"' "; 
			baseDao.deteleObjectbySql(sql);
			result.setState(Constant.HTTP_STATUS_OK);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ServiceExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
}
